package com.yksj.healthtalk.ui.login;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.StringFormatUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 找回密码
 * @author zhao
 *
 */
public class FindPaswdActivity extends BaseFragmentActivity implements OnClickListener{
	EditText mEditText1;
	EditText mEditText2;
	EditText mEditText3;
	EditText mEditText4;
	Button tokenButton;
	
	final Handler mHandler = new Handler(){
		int i = 40;
		public void handleMessage(android.os.Message msg) {
			i--;
			if(i <= 0){
				tokenButton.setText(getString(R.string.setting_huoqu_yanzhengma));
				tokenButton.setClickable(true);
				i = 60;
				return;
			}else{
				tokenButton.setText("剩余"+i+"s");
				sendEmptyMessageDelayed(0,1000);
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.login_find_paswd);
		iniUI();
	}
	
	private void iniUI(){
		initTitle();
		
		mEditText1 = (EditText)findViewById(R.id.editText1);
		mEditText2 = (EditText)findViewById(R.id.editText2);
		mEditText3 = (EditText)findViewById(R.id.editText3);
		mEditText4 = (EditText)findViewById(R.id.editText4);
		tokenButton = (Button)findViewById(R.id.get_token_btn);
		tokenButton.setOnClickListener(this);
		findViewById(R.id.sure_btn).setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.sure_btn:
			onComplete();
			break;
		case R.id.get_token_btn:
			onSendCode();
			break;
		}
	}
	
	private void onSendCode(){
		String name = mEditText1.getEditableText().toString();
		boolean isPhone = StringFormatUtils.isPhoneNumber(name);
		if(!isPhone){
			mEditText1.setError(getString(R.string.format_error));
			return;
		}
		tokenButton.setClickable(false);
		HttpRestClient.doHttpObtainVerificationCode(false,name,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				if("0".equals(content)){//还未绑定
					ToastUtil.showLong(FindPaswdActivity.this,getString(R.string.not_bund));
				}else{
					mHandler.sendEmptyMessage(0);
				}
			}
			@Override
			public void onFailure(Throwable error, String content) {
				tokenButton.setClickable(true);
				ToastUtil.showLong(FindPaswdActivity.this,"发送失败");
			}
			@Override
			public void onFinish() {
				tokenButton.setClickable(true);
			}
		});
		
	}
	
	private void onComplete(){
		String name = mEditText1.getEditableText().toString();
		if(name.length() == 0){
			mEditText1.setError("请输入手机号");
			return;
		}
		name = mEditText2.getEditableText().toString();
		if(name.length() == 0){
			mEditText2.setError("请输入验证码");
			return;
		}
		name = mEditText3.getEditableText().toString();
		if(name.length() == 0){
			mEditText3.setError("请输入新密码");
			return;
		}
		String repwd = mEditText4.getEditableText().toString();
		if(!repwd.equals(name)){
			mEditText4.setError("密码不匹配");
			return;
		}
		HttpRestClient.doHttpResetPaswd(mEditText1.getEditableText().toString(),
				mEditText2.getEditableText().toString(),
				mEditText3.getEditableText().toString(),
				new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, String content) {
					if(!isFinishing()){
						if("1".equals(content)){
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "修改成功");
						}else{
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),content);
						}
					}
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
					if(!isFinishing()){
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "修改失败");
					}
				}
		});
	}
}
